package com.sunnada.edu.business.contract.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.business.contract.service.ContractService;
import com.sunnada.edu.business.worker.service.WorkerMethods;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.pub.util.Utils;

/***
 * explain 合同控制层
 * 
 * @author chs 2015-5-12 version 1.0
 */
@Controller
@RequestMapping("/contractController")
public class ContractController extends BasicController {
	@Resource
	ContractService contractService;

	/** 合同首页 */
	@RequestMapping("/contract")
	public String contract(HttpServletRequest request) {
		String curr = request.getParameter("curr");
		request.setAttribute("curr", curr);
		// 当前路径
		String moduleId = request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		return "business/contract/contract";
	}

	/** 合同查询 */
	@RequestMapping("/contractSearch")
	public void contractSearch(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		Page page = new Page();
		page.setRowCount(contractService.getContractCots(params));
		page.setPageMap(request, page, params);
		List<?> contractList = contractService.findContracts(params);
		JSONObject json = new JSONObject();
		json.put("list", contractList);
		json.put("params", params);
		printInfo(response, json);
	}
	


	/** 合同添加页面 */
	@RequestMapping("/contractAdd")
	public String contractAdd(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		return "business/contract/contract_paper_add";
	}

	/** 合同修改页面 */
	@RequestMapping("/contractUpdate")
	public String contractUpdate(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		Map contract = contractService.getContractByRecId(params);
		request.setAttribute("contract", contract);
		return "business/contract/contract_paper_update";
	}
	
	
	/** 合同修改页面 */
	@RequestMapping("/contractDetail")
	public String contractDetail(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		Map contract = contractService.getContractByRecId(params);
		request.setAttribute("contract", contract);
		return "business/contract/contract_paper_detail";
	}
	
	/** 合同保存功能 */
	@RequestMapping("/contractSave")
	public void contractSave(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		String recId = params.get("recId");
		if (Utils.isNotNull(recId)) {
			msg = contractService.contractUpdate(request,params);
		} else {
			msg = contractService.contractAdd(request,params);
		}
		printInfo(response, msg);
	}

	/** 合同删除功能 */
	@RequestMapping("/contractDelete")
	public void contractDelete(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = contractService.contractDelete(params);
		printInfo(response, msg);
	}



	/** 合同停用 */
	@RequestMapping("/contractDisable")
	public void contractDisable(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = contractService.contractDisable(params);
		printInfo(response, msg);
	}

	/** 合同启用 */
	@RequestMapping("/contractEnable")
	public void contractEnable(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = contractService.contractEnable(params);
		printInfo(response, msg);
	}
	
	
	@RequestMapping("/selectWorkerPage")
	public String selectWorkerPage(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		return "business/contract/selectWorker";
	}
	
	/** 合同附件查询 */
	@RequestMapping("/getWorkerContracts")
	public void getWorkerContracts(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		List<?> list = WorkerMethods.getContractList(params);
		JSONObject json = new JSONObject();
		json.put("list", list);
		json.put("params", params);
		printInfo(response, json);
	}
}
